package com.example.Controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.Service.MenuService;
import com.example.Service.impl.MenuImpl;
import com.example.entity.Module;
import com.example.entity.Users;
import com.example.entity.vo.MoudleVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {
    @Autowired
    MenuImpl menuService;
    @GetMapping("/selectById")
    public SaResult selectById(@RequestParam Integer userId) {
        List<Module> menus = menuService.selectById(userId);
        if (menus != null) {
            List<Map<String, Object>> menuTree = menuService.buildCompleteMenuTree(menus);
            SaResult res = SaResult.data(menuTree);
            return res;
        }else {
            return SaResult.error("获取菜单失败");
        }
    }

    @PostMapping("/select")
    public SaResult select(@RequestBody MoudleVo moudleVo) {
        PageInfo<Module> menus = menuService.selectAllByPage(moudleVo);
        if (menus != null) {
         SaResult res = SaResult.data(menus);
          return res;
        }else {
            return SaResult.error("获取菜单失败");
        }
    }
    @PostMapping("/insert")
    public SaResult insert(@RequestBody Module module  ) {
        int i = menuService.insert(module);
        if(i>0){
            return SaResult.ok("新增成功");
        }else{
            return SaResult.error("新增失败");
        }
    }
    @PostMapping("/update")
    public SaResult update(@RequestBody  Module module) {
        int i = menuService.update(module);
        if(i>0){
            return SaResult.ok("编辑成功");
        }else{
            return SaResult.error("编辑失败");
        }
    }
    @RequestMapping("/deletById")
    public SaResult update(Integer moduleId) {
        int i = menuService.deletById(moduleId);
        if(i>0){
            return SaResult.ok("删除成功");
        }else{
            return SaResult.error("删除失败");
        }
    }
}
